/*
 * Copyright (C) 2011 Johan Kannala
 *
 * Licensed under the MIT License. You can find a copy of the License at in the
 * license.txt file.
 */
package com.joppe.vocabtutor.views;

import android.graphics.*;
import android.view.animation.*;

/**
 * @author joppe
 *
 */
public class FlipCardAnimation extends Animation {
    public enum FlipPhase {
        HideWord,
        ShowAnswer,
        HideAnswer,
        ShowWord;
    }
    private Camera mCamera = null;
    private float mCenterX = -1;
    private float mCenterY = -1;
    private float mStartAngle = -1;
    private float mEndAngle = -1;
    private FlipPhase mPhase = null;
    
    /**
     * 
     */
    public FlipCardAnimation(int duration, FlipPhase phase) {
        super();
        setDuration(duration);
        this.mPhase = phase;
    }
    
    /* (non-Javadoc)
     * @see android.view.animation.Animation#initialize(int, int, int, int)
     */
    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
        mCenterY = 0;
        mCenterX = width/2f;
//        setFillAfter(true);
//        setFillBefore(true);
        switch (mPhase) {
        case HideWord:
            setInterpolator(new AccelerateInterpolator());
            mStartAngle = 0;
            mEndAngle = 90;
            break;
        case ShowAnswer:
            setInterpolator(new DecelerateInterpolator());
            mStartAngle = -90;
            mEndAngle = 0;
            break;
        case HideAnswer:
            setInterpolator(new AccelerateInterpolator());
            mStartAngle = 0;
            mEndAngle = -90;
            break;
        case ShowWord:
            setInterpolator(new DecelerateInterpolator());
            mStartAngle = 90;
            mEndAngle = 0;
            break;
        }
    }
    
    /* (non-Javadoc)
     * @see android.view.animation.Animation#applyTransformation(float, android.view.animation.Transformation)
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Camera lCamera = mCamera;
        float lCenterX = mCenterX;
        float lCenterY = mCenterY;
        mCamera.save();
        lCamera.rotateY(mStartAngle + interpolatedTime*(mEndAngle - mStartAngle));
        Matrix m = t.getMatrix();
        lCamera.getMatrix(m);
        m.preTranslate(-lCenterX, -lCenterY);
        m.postTranslate(lCenterX, lCenterY);
        mCamera.restore();
    }
}